package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.nursing.domain.CheckInConfig;
import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.domain.dto.CheckInApplyDto;
import com.zzyl.nursing.domain.dto.CheckInConfigDto;
import com.zzyl.nursing.domain.dto.CheckInContractDto;
import com.zzyl.nursing.domain.dto.CheckInElderDto;
import com.zzyl.nursing.domain.pojo.Bed;
import com.zzyl.nursing.mapper.BedMapper;
import com.zzyl.nursing.mapper.CheckInConfigMapper;
import com.zzyl.nursing.mapper.ContractMapper;
import com.zzyl.nursing.mapper.ElderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.CheckInMapper;
import com.zzyl.nursing.domain.CheckIn;
import com.zzyl.nursing.service.ICheckInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入住Service业务层处理
 * 
 * @author alexis
 * @date 2025-09-27
 */
@Service
public class CheckInServiceImpl extends ServiceImpl<CheckInMapper, CheckIn> implements ICheckInService
{
    @Autowired
    private CheckInMapper checkInMapper;
    
    @Autowired
    private CheckInConfigMapper checkInConfigMapper;
    
    @Autowired
    private ContractMapper contractMapper;
    
    @Autowired
    private ElderMapper elderMapper;
    @Autowired
    private BedServiceImpl bedServiceImpl;
    @Autowired
    private BedMapper bedMapper;

    /**
     * 查询入住
     * 
     * @param id 入住主键
     * @return 入住
     */
    @Override
    public CheckIn selectCheckInById(Long id)
    {
        return checkInMapper.selectById(id);
    }

    /**
     * 查询入住列表
     * 
     * @param checkIn 入住
     * @return 入住
     */
    @Override
    public List<CheckIn> selectCheckInList(CheckIn checkIn)
    {
        return checkInMapper.selectCheckInList(checkIn);
    }

    /**
     * 新增入住
     * 
     * @param checkIn 入住
     * @return 结果
     */
    @Override
    public int insertCheckIn(CheckIn checkIn)
    {
        return checkInMapper.insert(checkIn);
    }

    /**
     * 修改入住
     * 
     * @param checkIn 入住
     * @return 结果
     */
    @Override
    public int updateCheckIn(CheckIn checkIn)
    {
        return checkInMapper.updateById(checkIn);
    }

    /**
     * 批量删除入住
     * 
     * @param ids 需要删除的入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInByIds(Long[] ids)
    {
        return checkInMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除入住信息
     * 
     * @param id 入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInById(Long id)
    {
        return checkInMapper.deleteById(id);
    }


    /**
     * 申请入住
     *
     * @param checkInApplyDto
     */
    @Override
    @Transactional
    public void apply(CheckInApplyDto dto) {

        //1.如果检验老人已入住,则提示"老人以入住
        CheckInElderDto checkInElderDto = dto.getCheckInElderDto();
        Elder enableElder = elderMapper.selectOne(Wrappers.<Elder>lambdaQuery().eq(Elder::getIdCardNo, checkInElderDto.getIdCardNo()).eq(Elder::getStatus, 1));
        //1.2如果判断状态为已入住,则提示老人已入住
        if (ObjectUtil.isNotEmpty(enableElder)) {
            throw new RuntimeException("老人已入住");
        }
        //2如果老人未入住
        //2.1-床:更新状态
        CheckInConfigDto checkInConfigDto = dto.getCheckInConfigDto();
        Long bedId = checkInConfigDto.getBedId();
        bedMapper.updateById(Bed.builder().id(bedId).bedStatus(1).build());


        //2.2-看老人新增/更新
        Elder disableElder = elderMapper.selectOne(Wrappers.<Elder>lambdaQuery().eq(Elder::getIdCardNo, checkInElderDto.getIdCardNo()).eq(Elder::getStatus, 1));
        Elder elder = BeanUtil.toBean(checkInElderDto, Elder.class);
        Bed bed = bedMapper.selectById(bedId);
        elder.setBedId(bedId);
        elder.setBedNumber(bed.getBedNumber());
        elder.setStatus(1);
        if (ObjectUtil.isEmpty(disableElder)) {
            elderMapper.insert(elder);
            elder.setBedId(elder.getId());
        } else {
            elderMapper.insert(elder);
        }
        //2.3-新增合同
        CheckInContractDto checkInContractDto = dto.getCheckInContractDto();
        Contract contract = BeanUtil.toBean(checkInContractDto, Contract.class);
        contract.setElderId(elder.getId());
        contract.setElderName(elder.getName());

        contract.setStartDate(checkInConfigDto.getStartDate());
        contract.setEndDate(checkInConfigDto.getEndDate());
        contract.setStatus(checkInConfigDto.getStartDate().isBefore(LocalDateTime.now())? 1:0);

        contract.setContractNumber(com.zzyl.common.utils.CodeGenerator.generateContractNumber());
        contractMapper.insert(contract);


        //2.4-入住和入住配置:新增

        CheckIn checkIn = new CheckIn();
        checkIn.setElderId(elder.getId());
        checkIn.setElderName(elder.getName());
        checkIn.setIdCardNo(elder.getIdCardNo());
        checkIn.setNursingLevelName(checkInConfigDto.getNursingLevelName());
        checkIn.setStartDate(checkInConfigDto.getStartDate());
        checkIn.setEndDate(checkInConfigDto.getEndDate());
        checkIn.setBedNumber(elder.getBedNumber());
        checkIn.setRemark(JSON.toJSONString(dto.getElderFamilyDtoList()));
        checkIn.setStatus(0);
        checkInMapper.insert(checkIn);
        //入住配置
        CheckInConfig checkInConfig = BeanUtil.toBean(checkInConfigDto, CheckInConfig.class);
        checkInConfig.setCheckInId(checkIn.getId());
        checkInConfigMapper.insert(checkInConfig);
    }
    /**
     * 确认入住
     *
     * @param checkInApplyDto
     */
 }


